package com.wolfalpha.utron.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.adapter.ItemTeamerAdapter;
import com.wolfalpha.utron.bean.Jlq;
import com.wolfalpha.utron.bean.Teamer;
import com.wolfalpha.utron.bean.Tlz;
import com.wolfalpha.utron.service.FriendsService;
import com.wolfalpha.utron.service.impl.FriendsServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.StringUtils;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.EditAlertDialog;
import com.wolfalpha.utron.view.ScrollListView;

import java.util.ArrayList;

import easemob.chatuidemo.activity.ChatActivity;

/**
 * Created by luodon on 15/9/12.
 */
public class TlzDetailActivity extends BaseNormalActivity {

    private boolean isQun = false;      //群和讨论组的判断标志
    private String tlzName = "";        //组名称
    private int tlzId = 0;

    private ScrollListView slTeamer;    //成员

    private TextView tvIntro;           //简介
    private TextView tvProName;         //项目名
    private TextView tvTime;            //创建时间
    private TextView tvNum;             //成员数量

    private LinearLayout llProject;     //项目信息

    private Button btnQuite;

    private FriendsService friendsService;

    private ItemTeamerAdapter adapter;
    private Jlq jlq;
    private Tlz tlz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("name")) {
            return;
        }
        if (bundle.containsKey("qun")) {
            isQun = true;
        }

        tlzName = bundle.getString("name");
        tlzId = bundle.getInt("id");

        setContentView(R.layout.activity_tlz_detail);

        friendsService = new FriendsServiceImpl();

        initView();
        loadData();
    }

    /**
     * 加载详细信息
     */
    private void loadData() {
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                if (isQun) {
                    jlq = friendsService.getJlqInfo(tlzId);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (jlq != null) {
                                tvProName.setText(jlq.getP_name());
                                tvIntro.setText(jlq.getIntro());
                                String num = "" + (jlq.getTeamer() == null ? 0 : jlq.getTeamer().size()) + " / " + jlq.getMax() + "";
                                tvNum.setText(num);
                                tvTime.setText(StringUtils.friendly_time(StringUtils.TimeStamp2Date(jlq.getTime() * 1000)));
                                if (jlq.getTeamer() != null) {
                                    adapter.setData(jlq.getTeamer());
                                }

                                btnQuite.setVisibility(View.VISIBLE);
                                btnQuite.setText((jlq.getIin() == 1 ? "退出项目群" : "申请加入项目群"));
                            } else {
                                UIHelper.ToastMessage(TlzDetailActivity.this, "获取信息失败");
                                finish();
                            }
                        }
                    });

                } else {
                    tlz = friendsService.getTlzInfo(tlzId);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (tlz != null) {
//                                tvProName.setText(tlz.getP_name());
                                tvIntro.setText(tlz.getIntro());
                                String num = "" + (tlz.getTeamer() == null ? 0 : tlz.getTeamer().size()) + "";
                                tvNum.setText(num);
                                tvTime.setText(StringUtils.friendly_time(StringUtils.TimeStamp2Date(tlz.getTime() * 1000)));
                                if (tlz.getTeamer() != null) {
                                    adapter.setData(tlz.getTeamer());
                                }
                                btnQuite.setVisibility(View.VISIBLE);
                                btnQuite.setText((tlz.getIin() == 1 ? "退出讨论组" : "加入讨论组"));
                            } else {
                                UIHelper.ToastMessage(TlzDetailActivity.this, "获取信息失败");
                                finish();
                            }
                        }
                    });
                }
            }
        });
    }

    String appStr = "申请加入群";
    String appStr1 = "申请加入讨论组";

    /**
     * 初始化界面
     */
    private void initView() {
        setTvTitle(isQun ? tlzName + "群" : tlzName + "讨论组");

        slTeamer = (ScrollListView) findViewById(R.id.slv_tlz_detail_teamer);
        tvIntro = (TextView) findViewById(R.id.tv_tlz_detail_intro);
        tvProName = (TextView) findViewById(R.id.tv_tlz_detail_pname);
        tvTime = (TextView) findViewById(R.id.tv_tlz_detail_time);
        tvNum = (TextView) findViewById(R.id.tv_tlz_detail_teamer_num);
        btnQuite = (Button) findViewById(R.id.btn_quit);

        llProject = (LinearLayout) findViewById(R.id.ll_tlz_detail_project);
        if (isQun) {
            llProject.setVisibility(View.VISIBLE);
        }

        adapter = new ItemTeamerAdapter(TlzDetailActivity.this, new ArrayList<Teamer>());
        slTeamer.setAdapter(adapter);

        btnQuite.setVisibility(View.GONE);


        btnQuite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isQun) {
                    if (jlq.getIin() == 1) {
                        //退出群
                        final ProgressDialog progressDialog = new ProgressDialog(TlzDetailActivity.this);
                        progressDialog.setMessage("正在退出...");
                        progressDialog.show();
                        new Thread(new Runnable() {
                            public void run() {
                                try {
                                    EMGroupManager.getInstance().exitFromGroup("jlq" + jlq.getId());
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            progressDialog.dismiss();
                                            finish();
                                            if (ChatActivity.activityInstance != null)
                                                ChatActivity.activityInstance.finish();
                                        }
                                    });
                                } catch (final Exception e) {
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            progressDialog.dismiss();
                                            Toast.makeText(getApplicationContext(), "退出失败:" + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }
                        }).start();
                    } else {
                        //申请加入
                        final EditAlertDialog editAlertDialog = new EditAlertDialog(TlzDetailActivity.this);
                        editAlertDialog.setHint("验证身份");
                        editAlertDialog.setMostWords(20);
                        editAlertDialog.setOnFinishedListener(new EditAlertDialog.OnFinishedListener() {
                            @Override
                            public void finish(String input) {
                                appStr = input;
                                new Thread(new Runnable() {
                                    public void run() {
                                        try {
                                            EMGroupManager.getInstance().applyJoinToGroup("jlq" + jlq.getId(), appStr);
                                            runOnUiThread(new Runnable() {
                                                public void run() {
                                                    UIHelper.ToastMessage(TlzDetailActivity.this, "发送成功");
                                                }
                                            });
                                        } catch (final EaseMobException e) {
                                            e.printStackTrace();
                                            runOnUiThread(new Runnable() {
                                                public void run() {
                                                    UIHelper.ToastMessage(TlzDetailActivity.this, "发送失败");
                                                }
                                            });
                                        }
                                    }
                                }).start();
                            }
                        });
                    }
                } else {
                    if (tlz.getIin() == 1) {
                        //退出讨论组
                        final ProgressDialog progressDialog = new ProgressDialog(TlzDetailActivity.this);
                        progressDialog.setMessage("正在退出...");
                        progressDialog.show();
                        new Thread(new Runnable() {
                            public void run() {
                                try {
                                    EMGroupManager.getInstance().exitFromGroup("tlz" + jlq.getId());
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            progressDialog.dismiss();
                                            finish();
                                            if (ChatActivity.activityInstance != null)
                                                ChatActivity.activityInstance.finish();
                                        }
                                    });
                                } catch (final Exception e) {
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            progressDialog.dismiss();
                                            Toast.makeText(getApplicationContext(), "退出失败:" + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }
                        }).start();
                    } else {
                        //加入讨论组
                        new Thread(new Runnable() {
                            public void run() {
                                try {
                                    EMGroupManager.getInstance().joinGroup("jlq" + jlq.getId());
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            UIHelper.ToastMessage(TlzDetailActivity.this, "加入成功");
                                        }
                                    });
                                } catch (final EaseMobException e) {
                                    e.printStackTrace();
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            UIHelper.ToastMessage(TlzDetailActivity.this, "加入失败");
                                        }
                                    });
                                }
                            }
                        }).start();
                    }
                }
            }
        });
    }


}
